// var URL = "http://csmjonline.iloveqipai.com:7000";

// 正式环境
// var URL = "http://pjonline.iloveqipai.com:7100";
// cc.VERSION = 20171207;           //正式版本号
//线上环境
var URL = "http://120.79.56.151:7100";
cc.VERSION = 20171212;  
// 测试环境
// var URL = "http://xunle.iloveqipai.com:7100";
// cc.VERSION = 20161227;           //测试版本号

var HTTP = cc.Class({
    extends: cc.Component,

    statics:{
        sessionId : 0,
        userId : 0,
        master_url:URL,
        url:URL,
        sendRequest : function(path,data,handler,extraUrl,callback,obj){
            var xhr = cc.loader.getXMLHttpRequest();
            var str = "?";
            if (callback&&obj) {
                obj.timeout = 15000;
                obj.isTimeOut=true;
                obj.callback=callback;
                obj.timeOutFn=function(){
                    if (obj.timeout>0&&obj.isTimeOut) {
                        obj.timeout-=1000;
                        console.log("正在请求");
                        setTimeout(obj.timeOutFn, 1000);
                    }else if(obj.timeout<=0){
                        obj.callback();
                        if (cc.vv.alert) {
                            cc.vv.alert.show("提示","请求超时，请重试")
                        }else if (cc.vv.wc) {
                            cc.vv.wc.show("请求超时，请重试")
                        }
                    }
                };
                obj.timeOutFn()
            };
            for(var k in data){
                if(str != "?"){
                    str += "&";
                }
                str += k + "=" + data[k]
            };
            if(extraUrl == null){
                extraUrl = HTTP.url
            };
            var requestURL = extraUrl + path + encodeURI(str);
            console.log("RequestURL:" + requestURL);
            xhr.open("GET",requestURL, true);
            if (cc.sys.isNative){
                xhr.setRequestHeader("Accept-Encoding","gzip,deflate","text/html;charset=UTF-8");
            };
            
            xhr.onreadystatechange = function() {
                if(xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 300)){
                    if (obj) {
                        obj.isTimeOut=false
                    };
                    
                    console.log("http res("+ xhr.responseText.length + "):" + xhr.responseText);
                    try {
                        var ret = JSON.parse(xhr.responseText);
                        console.log(ret);
                        if(handler !== null){
                            handler(ret);
                        }                        /* code */
                    } catch (e) {
                        console.log("err:" + e);
                        //handler(null);
                    }
                    finally{
                        if(cc.vv && cc.vv.wc){
                            //cc.vv.wc.hide();    
                        }
                    }
                }
            };
            
            if(cc.vv && cc.vv.wc){
                //cc.vv.wc.show();
            };
            xhr.send();
            return xhr
        },
    },
});